﻿using BLL;
using DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admincp_Suppliers : System.Web.UI.Page
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    SuppliersBLL s = new SuppliersBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadData();
            rbtnStatus.Enabled = false;
            btnUpdate.Enabled = false;
        }
    }

    private void loadData()
    {
        gvSuppliers.DataSource = s.getSupplier();
        gvSuppliers.DataBind();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        supplier sup = new supplier { 
            contactname=txtContactName.Text,
            contacttitle=txtContactTitle.Text,
            address=txtAddress.Text,
            city=txtCity.Text,
            postalcode=txtPostalCode.Text,
            country=txtCountry.Text,
            phone=txtPhone.Text,
            homepage=txtHomePage.Text
        };
        s.Suppliers_Insert(sup);
        this.loadData();
        this.reset();
    }
    public void reset() {
        txtContactName.Text = String.Empty;
        txtContactTitle.Text = String.Empty;
        txtAddress.Text = String.Empty;
        txtCity.Text = String.Empty;
        txtPostalCode.Text = String.Empty;
        txtCountry.Text = String.Empty;
        txtPhone.Text = String.Empty;
        txtHomePage.Text = String.Empty;
        rbtnStatus.Enabled = false;
        btnUpdate.Enabled = false;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        bool status;
        if (rbtnStatus.SelectedItem.Value == "1")
        {
            status = true;
        }
        else
        {
            status = false;
        }
        supplier sup = new supplier {
            id =int.Parse(gvSuppliers.SelectedRow.Cells[0].Text),
            contactname = txtContactName.Text,
            contacttitle = txtContactTitle.Text,
            address = txtAddress.Text,
            city = txtCity.Text,
            postalcode = txtPostalCode.Text,
            country = txtCountry.Text,
            phone = txtPhone.Text,
            homepage = txtHomePage.Text,
            status=status
        };
        s.Suppliers_Update(sup);
        this.loadData();
        this.reset();
    }
    protected void gvSuppliers_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int supid = int.Parse(gvSuppliers.Rows[e.NewSelectedIndex].Cells[0].Text);
        var danhsach = db.suppliers.SingleOrDefault(s1 => s1.id == supid);
        txtContactName.Text = danhsach.contactname;
        txtContactTitle.Text = danhsach.contacttitle;
        txtAddress.Text = danhsach.address;
        txtCity.Text = danhsach.city;
        txtPostalCode.Text = danhsach.postalcode;
        txtCountry.Text = danhsach.country;
        txtPhone.Text = danhsach.phone;
        txtHomePage.Text = danhsach.homepage;
        rbtnStatus.Enabled = true;
        btnUpdate.Enabled = true;
    }
    protected void gvSuppliers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvSuppliers.PageIndex = e.NewPageIndex;
        this.loadData();
    }
    protected void gvSuppliers_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int supplierid = int.Parse(gvSuppliers.Rows[e.RowIndex].Cells[0].Text);
        s.Suppliers_Delete(supplierid);
        this.loadData();
    }
}